// 滚动模式
export default {
  computed: {
    animateOptions() {
      // 默认动画配置
      const {
        delay = 0,
        direction = "up",
        hoverStop = true,
      } = this.animateConfig;
      // 计算首尾间距
      const spaceDistance = this.getSpaceDistance();
      // 移动距离
      const moveDistance = this.textHeight + spaceDistance;
      // 计算动画起始位置
      const { startDistance, endDistance } =
        this.getAnimateDistance(moveDistance);
      // 计算动画周期
      const moveDuration = this.getAnimateDuration(moveDistance);

      return {
        duration: moveDuration,
        delay,
        direction,
        spaceWidth: spaceDistance,
        hoverStop,
        startDistance,
        endDistance,
      };
    },
  },
  methods: {
    /**
     * # 获取首尾像素间距
     */
    getSpaceDistance() {
      const { spaceWidth = "50%" } = this.animateConfig;
      return typeof spaceWidth === "string"
        ? (parseFloat(spaceWidth) / 100) * this.textContainerHeight
        : spaceWidth;
    },
    /**
     * # 获取动画起始位置
     * @param moveDistance `number` 移动距离
     * @returns 动画起始位置
     */
    getAnimateDistance(moveDistance) {
      const { direction = "up" } = this.animateConfig;
      // 计算动画起始位置
      let startDistance;
      let endDistance;
      // 向右移动
      if (direction === "up") {
        startDistance = 0;
        endDistance = -moveDistance;
      } else {
        startDistance = -moveDistance;
        endDistance = 0;
      }
      return {
        startDistance,
        endDistance,
      };
    },
    /**
     * # 计算动画周期
     * @param moveDistance `number` 移动距离
     * @returns 动画周期
     */
    getAnimateDuration(moveDistance) {
      // 默认动画配置
      const { duration = 5, fixSpeed = 0 } = this.animateConfig;
      let moveDuration;
      if (fixSpeed > 0) {
        moveDuration = moveDistance / fixSpeed;
      } else {
        moveDuration = (this.textHeight / this.textContainerHeight) * duration;
      }
      return moveDuration;
    },
  },
};
